// AWTLabelPeer.cpp : implementation file
//

#include "stdafx.h"
//#include "AWTTest.h"
#include "AWTLabelPeer.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

IMPLEMENT_DYNAMIC(AWTLabelPeer, CStatic)

/////////////////////////////////////////////////////////////////////////////
// AWTLabelPeer

AWTLabelPeer::AWTLabelPeer(AWTComponent* comp)
{
	m_comp = comp;
	m_focus = FALSE;
}

AWTLabelPeer::~AWTLabelPeer()
{
//	if (m_hWnd != NULL)
//		DestroyWindow();
	if (m_hWnd != NULL)
	{
		HWND hwnd = m_hWnd;
		::DestroyWindow(hwnd);
		Detach();
	}
}


BEGIN_MESSAGE_MAP(AWTLabelPeer, CStatic)
	//{{AFX_MSG_MAP(AWTLabelPeer)
	ON_WM_CREATE()
	ON_WM_CTLCOLOR_REFLECT()
	ON_WM_DESTROY()
	ON_WM_KEYDOWN()
	ON_WM_CHAR()
	ON_WM_SETFOCUS()
	ON_WM_KILLFOCUS()
	ON_WM_PAINT()
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// AWTLabelPeer message handlers

int AWTLabelPeer::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CStatic::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	m_AWTWindow = getAWTWindow(this);
/*
	CWnd* wnd = this;
	CString str;
	BOOL bFound = FALSE;
	do {
		wnd = wnd->GetParent();
		wnd->GetWindowText(str);
		if (str == "!~AWTWindow~!")
		{
			bFound = TRUE;
			break;
		}
		else if (wnd->IsKindOf(RUNTIME_CLASS(AWTDialogPeer)))
		{
			bFound = TRUE;
			break;
		}
		else if (wnd->IsKindOf(RUNTIME_CLASS(AWTMdiChildWnd)))
		{
			bFound = TRUE;
			break;
		}
	} while (wnd != NULL);
	if (bFound)
		m_AWTWindow = wnd;		
*/
	return 0;
}

HBRUSH AWTLabelPeer::CtlColor(CDC* pDC, UINT nCtlColor) 
{
	AWTColor* fg = m_comp->getForeground();
	AWTColor* bk = m_comp->getBackground();
	CBrush* br = m_comp->getBkBrush();

	if (fg != NULL)
		pDC->SetTextColor(fg->getColorRef());
	if (bk != NULL)
		pDC->SetBkColor(bk->getColorRef());
	return (HBRUSH)*br;
}

void AWTLabelPeer::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	time_t t = 0;
#ifndef UNDER_CE
	time(&t);
#endif
	AWTKeyEvent e(m_comp, AWTKeyEvent::KEY_PRESSED, (long)t, nFlags, nChar, nChar);
	m_comp->processKeyEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTKEYEVENT, 0, (LPARAM)&e);
	CStatic::OnKeyDown(nChar, nRepCnt, nFlags);
}

void AWTLabelPeer::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	if (nChar == '\t')
		return;
	
	CStatic::OnChar(nChar, nRepCnt, nFlags);
}

void AWTLabelPeer::OnSetFocus(CWnd* pOldWnd) 
{
	CStatic::OnSetFocus(pOldWnd);
	
	AWTFocusEvent e(m_comp, AWTFocusEvent::FOCUS_GAINED);
	m_comp->processFocusEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTFOCUSEVENT, 0, (LPARAM)&e);
	m_focus = TRUE;
	Invalidate();
}

void AWTLabelPeer::OnKillFocus(CWnd* pNewWnd) 
{
	CStatic::OnKillFocus(pNewWnd);
	
	AWTFocusEvent e(m_comp, AWTFocusEvent::FOCUS_LOST);
	m_comp->processFocusEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTFOCUSEVENT, 0, (LPARAM)&e);
	m_focus = FALSE;
	Invalidate();
}

void AWTLabelPeer::OnPaint() 
{
	CString txt;
	txt.TrimRight();
	if (!txt.IsEmpty() || !m_focus)
	{
		CStatic::OnPaint();
		return;
	}

	CPaintDC dc(this); // device context for painting
	
	CFont* ftOld = dc.SelectObject(CFont::FromHandle(m_comp->getFont()));
	CSize sz = dc.GetTextExtent("WWWy");
	dc.SelectObject(ftOld);

	if (m_focus)
	{
		RECT rcClient;
		GetClientRect(&rcClient);
		int top = (rcClient.bottom - sz.cy) / 2;
		RECT rc = {0, top, sz.cx, top + sz.cy};
		dc.DrawFocusRect(&rc);
	}
	// Do not call CStatic::OnPaint() for painting messages
}

void AWTLabelPeer::OnDestroy() 
{
	CStatic::OnDestroy();
	AWTWindowEvent e(m_comp, AWTWindowEvent::WINDOW_CLOSED);
//	m_comp->processWindowEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTWINDOWEVENT, 0, (LPARAM)&e);
}
